<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class OrderSoundRecording
 * Desc  行程录音控制器
 * Create on 2025/4/25 15:51
 * Create by wangyafang
 */

namespace app\api\controller\wddj;


use app\api\model\wddj\Config;
use app\api\model\wddj\User;
use app\common\controller\Api;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use Exception;

class OrderSoundRecording extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\OrderSoundRecording();
    }

    /**
     * Desc 提交录音
     * Create on 2025/4/25 15:54
     * Create by wangyafang
     */
    public function submit()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $configObj = (new Config())->get(1);
        if ($configObj['sound_recording_status'] != 1) {
            $this->error('行程录音后台未启用');
        }
        $param = $this->request->post();
        $result = $this->validate($param,'app\api\validate\wddj\OrderSoundRecording');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $wechat_id = User::getWechatId($this->auth->id);
        $where = array();
        if ($param['source'] == 1) {
            $param['user_wechat_id'] = $wechat_id;
            $orderObj = (new \app\api\model\wddj\driver\Order())
                ->where('id',$param['order_id'])
                ->where('wechat_id',$wechat_id)
                ->find();
            $where['user_wechat_id'] = array('eq',$wechat_id);
        } else {
            $driverObj = (new \app\api\model\wddj\driver\Driver())->where('wechat_id',$wechat_id)->find();
            if (!$driverObj) {
                $this->error('不是司机，没有权限');
            }
            $param['driver_id'] = $driverObj['id'];
            $param['driver_wechat_id'] = $wechat_id;
            $where['driver_wechat_id'] = array('eq',$param['driver_wechat_id']);

        }

        $orderObj = (new \app\api\model\wddj\driver\Order())
            ->where('id',$param['order_id'])
            ->where('wechat_id',$wechat_id)
            ->find();

        if (!$orderObj) {
            $this->error('订单不存在');
        }
        if ($orderObj['driver_arrive_status'] != 'arrived') {
            $this->error('未到达指定地点，无法录音');
        }
        $queryObj = $this->model
            ->where('order_id',$param['order_id'])
            ->where('url',$param['url'])
            ->where($where)
            ->find();
        if ($queryObj) {
            $this->error('已上传行程录音');
        }
        $sound_recording_expire_day = $configObj['sound_recording_expire_day'];
        $current_time = time(); // 获取当前时间戳
        $param['expire_time'] = strtotime("+$sound_recording_expire_day days", $current_time);
        $uploadConfig = \think\Config::get("upload");
        $param['storage'] = $uploadConfig['storage'];
        $param['start_time'] = strtotime($param['start_time']);
        $param['end_time'] = strtotime($param['end_time']);
        Db::startTrans();
        try {
            $this->model->data($param);
            $this->model->allowField(true)->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('提交成功');
    }
}



 